package falstad;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Color;

public class MazePanel extends Panel  {
	/* Panel operates a double buffer see
	 * http://www.codeproject.com/Articles/2136/Double-buffer-in-standard-Java-AWT
	 * for details
	 */
	Image bufferImage ;
	Graphics gc;
	Color c;
	
	public MazePanel() {
		super() ;
		this.setFocusable(false) ;
	}
	@Override
	public void update(Graphics g) {
		paint(g) ;
	}
	@Override
	public void paint(Graphics g) {
		g.drawImage(bufferImage,0,0,null) ;
	}
	/*
	public void setBufferImage(Image buffer) {
		bufferImage = buffer ;
	}
	*/
	public void initBufferImage() {
		bufferImage = createImage(Constants.VIEW_WIDTH, Constants.VIEW_HEIGHT);
		if (null == bufferImage)
		{
			System.out.println("Error: creation of buffered image failed, presumedly container not displayable");
		}
	}
	public Graphics getBufferGraphics() {
		if (null == bufferImage)
			initBufferImage() ;
		return bufferImage.getGraphics() ;
	}
	public void update() {
		paint(getGraphics()) ;
	}
	
	
	public void makeNewGraphics()
	{
		
	}
	
	public void fillPolygon(int[] xps, int[] yps, int val)
	{
		gc = getBufferGraphics();
		gc.fillPolygon(xps, yps, val);
	}
	public void updateGraphics(int view_width, int view_height) {
		gc = getBufferGraphics();
		gc.setColor(Color.black);
		gc.fillRect(0, 0, view_width, view_height/2);
		// draw dark gray background on upper half
		gc.setColor(Color.darkGray);
		gc.fillRect(0, view_height/2, view_width, view_height/2);
		// set color to white and draw what ever can be seen from the current position
		gc.setColor(Color.white);
		
	}
	
	public void setColor(Color c)
	{
		gc = getBufferGraphics();
		gc.setColor(c);
	}
	
//	public Color getColor()
//	{
//		return c;
//	}
	
	public Color generateColor(int[] raw)
	{
		c = new Color(raw[0], raw[1], raw[2]);
		return c;
	}
	
	//public void drawMap()
	
//	public void redraw(Graphics gc, int state, int px, int py, int view_dx, int view_dy, int walk_step, int view_offset, RangeSet rset, int ang)
//	{
//		//TODO: FILL THIS IN
//	}
}
